/*
 * Utils.h
 *
 *  Created on: Feb 9, 2012
 *      Author: shaohong
 */

#ifndef UTILS_H_
#define UTILS_H_

#include <sys/types.h>
#include <unistd.h>
#include <string>
#include <vector>

#include <iostream>
#include <cctype>
#include <cwctype>
#include <stdint.h>

#include "Hop.h"
using namespace std;


class Utils {
public:
	Utils();
	virtual ~Utils();

	static int32_t getFileSize(const char * fName);

	//checks if we see a blank line
	static bool isBlankLine(string & aString);

	// write the output buffer to the fd
	static int flushOutputBuffer(int clientSocket, char * outputBuffer, int outputBufferDataSize);

	static bool fileExists(string fileName);

	/**
	 * parse the hopConfigStr such as "1.2.3.4:5678,2.3.4.5:80"
	 */
	static void parseHops(string hopConfigStr, vector<Hop*> & nextHops );

	static string parseHostInHostPortStr(string hostPortStr);

	static int parsePortInHostPortStr(string hostPortStr);

	static inline bool caseInsCharCompareN(char a, char b) {
	   return(toupper(a) == toupper(b));
	}

	static bool caseInsCompare(const string& s1, const string& s2);

	static string int2Str(int number);

	/**
	 * Copy nBytes of bytes from readingFD to writingFDs. In a best effort way
	 */
	static void copyByteStreams(int readingFD, vector<int>writingFDs, uint32_t nBytes);

	static void setupSigHandler(int sigNo, void(*sigHandler)(int));

	static void daemonize(void);

	/* a wrapper on read(int fd, void *buf, size_t count);
	 * with an additional timeout value
	 * The timeout is in milliseconds
	 */
	static ssize_t readWithTO(int fd, void *buf, size_t count, uint32_t timeout);

	/**
	 * a wrapper function on ssize_t write(int fd, const void *buf, size_t count);
	 * the timeout is in milliseconds
	 */
	static ssize_t writeWithTO(int fd, const void *buf, size_t count, uint32_t timeout);

	static const uint32_t TO_Default = 5*1000;

	static string toUpperCaseStr(string str);
};

#endif /* UTILS_H_ */
